/*
 * @(#)MainMenu.java    1.0
 *
 * Copyright 2009 GFE, All rights reserved.
 * Game For Education.
 */
package egfe;

import java.io.IOException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 * Class <code>GFEInfo</code> adalah sebuah class yang menangani informasi yang
 * diberikan oleh pembuat game kepada player sebelum memainkan gamenya seperti
 * petunjuk penggunaan, keterangansecara mendetail, serta about yang biasanya sering
 * dijumpai dalam sebuah permainan. Class ini mengimplement MainMenuAction supaya
 * dapat dieksekusi pada MainMenu.
 *
 * @author  Sajarwo Anggai
 * @author  Abdul Rizal Adompo
 * @version 1.0, 11 May 2009
 * @see	    MainMenu
 * @see	    GFEInfo
 */
public class GFEInfo extends Canvas implements MainMenuAction {

    private Display display;
    private Displayable next;
    private int width,  height,  headerHeight,  footerHeight,  bodyHeight;
    private Image header,  footer,  body,  up,  down;
    private int inc = 1;
    private int carry,  incBody;

    public GFEInfo(Display display, Displayable next) {
        this.display = display;
        this.next = next;
    }

    /**
     *
     * @param header image untuk header
     */
    public void setHeader(Image header) {
        this.header = header;
        this.headerHeight = header.getHeight();
    }

    /**
     *
     * @param body image untuk body
     */
    public void setBody(Image body) {
        this.body = body;
        this.bodyHeight = body.getHeight();
    }

    /**
     *
     * @param footer image untk footer
     */
    public void setFooter(Image footer) {
        this.footer = footer;
        this.footerHeight = footer.getHeight();
    }

    private void reload() {
        this.width = getWidth();
        this.height = getHeight();
        try {
            up = Image.createImage(this.getClass().getResourceAsStream("env/atas.png"));
            down = Image.createImage(this.getClass().getResourceAsStream("env/bawah.png"));
            carry = (height - headerHeight - footerHeight - bodyHeight);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    protected void paint(Graphics g) {
        g.setColor(255, 255, 255);
        g.fillRect(0, 0, getWidth(), getHeight());
        if (body != null) {
            g.drawImage(body, 0, headerHeight + incBody, 0);
        }
        if (header != null) {
            g.drawImage(header, 0, 0, 0);
        }
        if (footer != null) {
            g.drawImage(footer, 0, height - footerHeight, 0);
        }
        if (carry < 0) {
            g.drawImage(up, getWidth() - 10, headerHeight, 0);
            g.drawImage(down, getWidth() - 10, height - footerHeight - down.getHeight(), 0);
        }
    }

    protected void keyPressed(int keyCode) {
        keyRepeated(keyCode);
    }

    protected void keyRepeated(int keyCode) {
        System.out.println(incBody + " =" + carry);
        if ((keyCode == -1 || keyCode == 50) && incBody < 0) {
            incBody = Math.min(incBody + inc, 0);
        } else if ((keyCode == -2 || keyCode == 56) && incBody >= carry) {
            incBody -= Math.min(inc, incBody - carry);
        } else if (keyCode == -7) {
            display.setCurrent(next);
        }
        repaint();
    }

    /**
     *
     * @param inc kecepatan scrolling
     */
    public void setIncremen(int inc) {
        this.inc = inc;
    }

    public void action(int action) {
        this.incBody = 0;
        display.setCurrent(this);
        setFullScreenMode(true);
        reload();
    }
}
